package com.zdsoft.finance.demo.service.impl;

import com.zdsoft.finance.demo.service.DClientSevice;
import com.zdsoft.finance.demo.entity.DClient;
import com.zdsoft.finance.demo.repository.DClientRepository;
import com.zdsoft.framework.core.common.util.ObjectHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.List;

/**
 * 客户业务实现类
 * Created on 2016-09-18
 * @author Maple
 * @version 1.0
 */
@Service
public class DClientSeviceImpl implements DClientSevice {

    @Autowired
    private DClientRepository dClientRepository;
    @Override
    public Page<DClient> queryAllByName(String name, PageRequest pageRequest) {
        if(ObjectHelper.isEmpty(name)){
             //TODO 抛出异常
        }
        dClientRepository.findAll(new Specification<DClient>() {
            @Override
            public Predicate toPredicate(Root<DClient> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.conjunction();
                    //获取关联对象
                  //Path<String> idPath = root.get("channel").get("id");
                  // 获取条件表达式
                  //List<Expression<Boolean>> expressions = predicate.getExpressions();
                //添加条件
                 //expressions.add(cb.like(root.<String>get("keyword"), "%"+keyword+"%"));           //关键字

                return predicate;
            }
        });
        return dClientRepository.findByNameLike("%"+name+"%",pageRequest);
    }

    @Transactional
    @Override
    public DClient save(DClient dClient) {
        return dClientRepository.save(dClient);
    }

    @Transactional
    @Override
    public void delete(String id) {
        dClientRepository.delete(id);
    }

    @Transactional
    @Override
    public void delete(DClient dClient) {
        dClientRepository.delete(dClient);
    }
}
